﻿using System;
using System.Linq;
using Anmlz.Model.Entities;
using Anmlz.Model.Model;
using Anmlz.Services;

namespace Anmlz.Data.ModelFactory
{
    public partial class ModelFactory
    {
        public Country Parse(CountryModel model, bool creation)
        {
            try
            {
                Country entity = null;

                if (!creation)
                {
                    entity = model.Id == 0 ?
                        this._unitOfWork.GetCountryByName(model.Name) :
                        this._unitOfWork.Countries.GetById(model.Id);
                }
                if (entity == null)
                {
                    entity = new Country();
                }

                switch (_languageService.GetCurrentLanguage())
                {
                    case CultureNames.English:
                        entity = _unitOfWork.Countries.All.First(c => c.NameEn == model.Name);
                        break;
                    case CultureNames.Russian:
                        entity = _unitOfWork.Countries.All.First(c => c.NameRu == model.Name);
                        break;
                }

                switch (_languageService.GetCurrentLanguage())
                {
                    case CultureNames.English:
                        entity.NameEn = model.Name;
                        break;
                    case CultureNames.Russian:
                        entity.NameRu = model.Name;
                        break;
                }

                return entity;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}